const app = getApp();
var url = app.data.url;
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    desk: [],
    tips: "提示:选择房间或餐台后请点击锁定!",
    uuid: "",
    merchantId: '',
    show: false,
    name: '',
    itemId: "",
    checked: false,
    btnType: 0,
    radio: "1",
    id:'',
    unitList:[],
    title:'',
    detailId: '',
    detailName:'',
    specPrice: '',
    specificationId: '',
    specDetal:'',
    unitDetailList: '',
    btnDetailType: 0,
    itemSpecRadio: '0',
  },
  radioOnChange(event) {
    this.setData({
      itemSpecRadio: event.detail,
    });
  },

  radioOnClick(event) {
    const { name } = event.currentTarget.dataset;
    this.setData({
      itemSpecRadio: name,
    });
  },
  changeBtn: function(){
    let _that = this;
    _that.setData({
      btnDetailType: 0,
      detailId: '',
      detailName: '',
      specPrice: ''
    })
  },
  btnClickChange: function (option) {
    let _that = this;
    let detail = option.currentTarget.dataset.detail;
    let detailId = detail.id;
    let detailName = detail.name;
    let specPrice = detail.specPrice;
    _that.setData({
      detailId: detailId,
      detailName: detailName,
      specPrice: specPrice,
      btnDetailType: 1
    })
  },
  insertOrUpdateDetail: function (option) {
    let _that = this;
    let name = _that.data.detailName;
    let merchantId = _that.data.merchantId;
    let specPrice = _that.data.specPrice;
    let uuid = _that.data.uuid;
    let itemId = _that.data.itemId;
    let method = option.currentTarget.dataset.method;
    let tempUrl;
    let tempData;
    if (method == 0) {
      tempUrl = url + '/ItemSystemApi/insertItemSpecDetail';
      tempData = {
        name: name,
        specPrice: specPrice,
        merchantId: merchantId,
        itemId: itemId,
        specificationId: _that.data.specificationId,
        createUserName: uuid
      }
    } else {
      tempUrl = url + '/ItemSystemApi/editItemSpecDetail';
      let id = _that.data.detailId;
      tempData = {
        id: id,
        name: name,
        specPrice: specPrice,
        merchantId: merchantId,
        itemId: itemId,
        specificationId: _that.data.specificationId,
        createUserName: uuid
      }
    }
    console.log(tempData);
    wx.request({
      url: tempUrl,
      data: tempData,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.code == 200) {
          _that.setData({
            show: false
          })
          _that.queryDataForDetail(_that.data.specificationId);
          wx.showToast({
            title: '操作成功!',
            icon: "success",
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }

      },
      fail: function (res) {
        console.info('submit fail');
      },
      complete: function (res) {
        console.info('submit complete');
      }
    })

  },
  /**
   * 删除
   */
  deleteUnitDetail: function (option) {
    let _that = this;
    let id = _that.data.detailId;
    wx.request({
      url: url + '/ItemSystemApi/deleteItemSpecDetail',
      data: {
        id: id,
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.code == 200) {
          _that.setData({
            detailId: '',
            detailName: '',
            specPrice:''
          })
          _that.queryDataForDetail(_that.data.specificationId);
          wx.showToast({
            title: "删除成功!",
            icon: "success",
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }

      },
      fail: function (res) {
        console.info('submit fail');
      },
      complete: function (res) {
        console.info('submit complete');
      }
    })
  },
  insertOrUpdate: function(option) {
    let _that = this;
    let name = _that.data.name;
    let merchantId = _that.data.merchantId;
    let uuid = _that.data.uuid;
    let itemId = _that.data.itemId;
    let method = option.currentTarget.dataset.method;
    let selectStatus = _that.data.itemSpecRadio;
    let tempUrl;
    let tempData;
    if (method == 0) {
      tempUrl = url + '/ItemSystemApi/insertItemSpec';
      tempData = {
        name: name,
        merchantId: merchantId,
        itemId: itemId,
        createUserName: uuid,
        selectStatus: selectStatus
      }
    } else {
      tempUrl = url + '/ItemSystemApi/editItemSpec';
      let id = _that.data.specificationId;
      tempData = {
        id: id,
        name: name,
        merchantId: merchantId,
        itemId: itemId,
        createUserName: uuid,
        selectStatus: selectStatus
      }
    }
    wx.request({
      url: tempUrl,
      data: tempData,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (res.data.code == 200) {
          _that.setData({
            show: false
          })
          _that.queryData(itemId);
          wx.showToast({
            title: '操作成功!',
            icon: "success",
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }

      },
      fail: function(res) {
        console.info('submit fail');
      },
      complete: function(res) {
        console.info('submit complete');
      }
    })

  },
  /**表单 start */
  onNameChange: function(option) {
    let _that = this;
    _that.setData({
      name: option.detail
    })
  },
  onSpecPriceChange: function (event) {
    let _that = this;
    _that.setData({
      specPrice: event.detail
    })
  },
  onDetaiLNameChange: function (event) {
    let _that = this;
    _that.setData({
      detailName: event.detail
    })
  },
  onRadioChange: function (event) {
    console.log(event);
    let _that = this;
    this.setData({
      radio: event.detail
    });
  },
  onChange: function (event) {
    this.setData({
      checked: event.detail
    });
  },
  /**
   * 删除
   */
  deleteUnit: function(option){
    let _that = this;
    let id = _that.data.specificationId;
    wx.request({
      url: url + '/ItemSystemApi/deleteItemSpec',
      data: {
        id:id
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.code == 200) {
          _that.setData({
            show: false
          })
          let itemId = _that.data.itemId;
          _that.queryData(itemId);
          wx.showToast({
            title: "删除成功!",
            icon: "success",
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }

      },
      fail: function (res) {
        console.info('submit fail');
      },
      complete: function (res) {
        console.info('submit complete');
      }
    }) 
  },
  /**表单 end */
  openUnitPop: function(option) {
    console.log(option);
    let _that = this;
    let type = option.currentTarget.dataset.type;
    if(type == 1){
      let name = option.currentTarget.dataset.item.name;
      let id = option.currentTarget.dataset.item.id;
      let selectStatus = option.currentTarget.dataset.item.selectStatus;
      console.log(selectStatus);
      _that.setData({
        specificationId: id,
        name: name,
        itemSpecRadio: selectStatus + ""
      })
    }
    _that.setData({
      btnType: type,
      show: true
    })
  },
 
  onClose() {
    this.setData({
      show: false
    });
  },

  onDetailShow: function (option) {
    let _that = this;
    console.log(option);
    let item = option.currentTarget.dataset.item;
    let type = option.currentTarget.dataset.type
    _that.queryDataForDetail(item.id);
    this.setData({
      btnDetailType: type,
      name: item.name,
      specificationId: item.id,
      detailShow: true,
      detailName: '',
      specPrice: ''
    });
  },
  onDetailClose: function () {
    this.setData({
      detailShow: false
    });
  },
  onLoad: function(options) {
    console.log(options);
    let _that = this;
    let title = options.title;
    let itemId = options.itemId
    let merchantId = options.merchantId;
    wx.getStorage({
      key: "uuid",
      success(res) {
        console.log(res);
        _that.setData({
          uuid: res.data
        })
      }
    })
    _that.queryData(itemId);
    _that.setData({
      title: title,
      itemId: itemId,
      merchantId: merchantId
    })

  },
  queryDataForDetail: function (specId) {
    let _that = this;
    wx.request({
      url: url + '/ItemSystemApi/listPageSpecDetail',
      data: {
        specId: specId
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.code == 200) {
          console.log(res.data.data);
          _that.setData({
            unitDetailList: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }
      },
      fail: function (res) {
        console.info('submit fail');
      },
      complete: function (res) {
        console.info('submit complete');
      }
    })

  },
  queryData: function(itemId){
    let _that = this;
    wx.request({
      url: url + '/ItemSystemApi/listPageSpec',
      data: {
        itemId: itemId
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.code == 200) {
          console.log(res.data.data);
          _that.setData({
            unitList: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }
      },
      fail: function (res) {
        console.info('submit fail');
      },
      complete: function (res) {
        console.info('submit complete');
      }
    })
    
  },
  changeVal: function(e) {
    let _that = this;
    _that.setData({
      value: e.detail
    })
  },



})